package com.frx.datastruct.chaptereleven;

/**
 * Created by xurenfeng on 2019/3/14.
 */
public class UnionFind1 implements UF{

    private int[] id;

    public UnionFind1(int size){
        id = new int[size];
        for (int i = 0;i < size;i++){
            id[i] = i;
        }
    }

    private int find(int index){
        if (index < 0 || index >= id.length){
            throw new IllegalArgumentException("index is illegal !");
        }
        return id[index];
    }
    @Override
    public int getSize() {
        return id.length;
    }

    @Override
    public boolean isConnected(int p, int q) {

        return find(p) == find(q);
    }

    @Override
    public void unionElement(int p, int q) {

        int pID = find(p);
        int qID = find(q);
        if (pID == qID){
            return;
        }

        for (int i = 0 ; i<id.length;i++){
            if (id[i] == pID){
                id[i] = qID;
            }
        }
    }
}
